﻿//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      LogInfo Helper
// Created By:     Samara Jiang (samaraj@wicresoft.com)
// Created Date:   2011-2-9
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASBCC.Database.EntityFramework;

namespace ASB.CC.Business.CIS
{
    /// <summary>
    ///  LogInfo Helper
    /// </summary>
    public static class LogInfoHelper
    {
        /// <summary>
        /// 通过roleID获取Pages
        /// </summary>
        public static IEnumerable GetPageWithRole(int roleID)
        {
            using (var dc = new CCEntities())
            {
                var role = dc.Roles.FirstOrDefault(p => p.RoleID == roleID);

                StringBuilder sb = new StringBuilder();

                role.AspxPages.Load();
                foreach (var c in role.AspxPages.Where(p => p.Disabled == false))
                {
                    sb.AppendFormat("{0},", c.FullName);
                }

                if (sb.Length > 0)
                {
                    return sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }
            }
        }
        /// <summary>
        /// 通过roleID获取Menus
        /// </summary>
        public static IEnumerable GetMenuWithRole(int roleID)
        {
            using (var dc = new CCEntities())
            {
                var role = dc.Roles.FirstOrDefault(p => p.RoleID == roleID);

                StringBuilder sb = new StringBuilder();

                role.Menus.Load();
                foreach (var c in role.Menus.Where(p => p.Disabled == false))
                {
                    sb.AppendFormat("{0},", c.Name);
                }

                if (sb.Length > 0)
                {
                    return sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }
            }
        }
        /// <summary>
        /// 通过roleID获取PCAT
        /// </summary>
        public static IEnumerable GetPCATWhthRole(int roleID)
        {
            using (var dc = new CCEntities())
            {
                var role = dc.Roles.FirstOrDefault(p => p.RoleID == roleID);

                StringBuilder sb = new StringBuilder();

                role.PCATs.Load();
                foreach (var c in role.PCATs)
                {
                    var provicne = dc.Provinces.FirstOrDefault(p => p.ProvinceID == c.ProvinceID).Name;
                    var category = dc.Categories.FirstOrDefault(p => p.CateID == c.CateID).Name;

                    sb.AppendFormat("{0}{1}, ", provicne.ToString(), category.ToString());
                }
                if (sb.Length > 0)
                {
                    return sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }

            }
        }

        /// <summary>
        /// 通过roleID获取Country
        /// </summary>
        public static IEnumerable GetCountryWithRole(int roleID)
        {
            using (var dc = new CCEntities())
            {
                var role = dc.Roles.FirstOrDefault(p => p.RoleID == roleID);
                StringBuilder sb = new StringBuilder();
                role.Countries.Load();
                foreach (var c in role.Countries.Where(p => !p.Disabled))
                {
                    sb.AppendFormat("{0},", c.Name);
                }
                if (sb.Length > 0)
                {
                    return sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }

            }

        }


        /// <summary>
        /// 获取用户角色所对应的权限
        /// </summary>
        public static IEnumerable GetUserRole(string[] RoleIDs)
        {

            using (var dc = new CCEntities())
            {
                string belongsTo = "";
                foreach (var roleid in RoleIDs)
                {
                    if (String.IsNullOrEmpty(roleid))
                        continue;
                    int roleid1 = int.Parse(roleid);
                    var role = dc.Roles.FirstOrDefault(p => p.RoleID == roleid1);
                    if (role.BelongsTo == "CID")
                    {
                        belongsTo = "CID";
                    }
                    if (role.BelongsTo == "CIO")
                    {
                        belongsTo = "CIO";
                    }
                }
                return belongsTo;

            }
        }

        /// <summary>
        /// 通过cateID获取Department
        /// </summary>
        public static IEnumerable GetDepartmentWithCAT(int cateID)
        {
            using (var dc = new CCEntities())
            {
                var category = dc.Categories.FirstOrDefault(p => p.CateID == cateID);
                StringBuilder sb = new StringBuilder();
                category.Departments.Load();
                foreach (var c in category.Departments.Where(p => p.Disabled == false || !p.Disabled.HasValue))
                {
                    sb.AppendFormat("{0},", c.DepartmentNameCN);
                }
                if (sb.Length > 0)
                {
                    return sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }

            }

        }

        /// <summary>
        /// 通过clusterID获取Province
        /// </summary>
        public static IEnumerable GetProvinceWithCluster(int clusterID)
        {
            using (var dc = new CCEntities())
            {
                var cluster = dc.Clusters.FirstOrDefault(p => p.ClusterID == clusterID);
                StringBuilder sb = new StringBuilder();
                cluster.Provinces.Load();
                foreach (var c in cluster.Provinces.Where(p => !p.Disabled))
                {
                    sb.AppendFormat("{0},", c.Name);
                }
                if (sb.Length > 0)
                {
                    return sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }

            }

        }

        /// <summary>
        /// 通过companyID获取PCAT
        /// </summary>
        public static IEnumerable GetPCATWhthCompany(int companyID)
        {
            using (var dc = new CCEntities())
            {
                var company = dc.Companies.First(p => p.CompanyID == companyID);

                StringBuilder sb = new StringBuilder();

                company.PCATs.Load();
                foreach (var c in company.PCATs)
                {
                    var provicne = dc.Provinces.First(p => p.ProvinceID == c.ProvinceID).Name;
                    var category = dc.Categories.First(p => p.CateID == c.CateID).Name;

                    sb.AppendFormat("{0}{1}, ", provicne.ToString(), category.ToString());
                }
                if (sb.Length > 0)
                {
                    return sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }

            }
        }

        /// <summary>
        /// 通过provinceID获取Department
        /// </summary>
        public static IEnumerable GetDepartmentWithProvince(int provinceID)
        {
            using (var dc = new CCEntities())
            {
                var province = dc.Provinces.FirstOrDefault(p => p.ProvinceID == provinceID);
                StringBuilder sb = new StringBuilder();
                province.Departments.Load();
                foreach (var c in province.Departments.Where(p => p.Disabled == false || !p.Disabled.HasValue))
                {
                    sb.AppendFormat("{0},", c.DepartmentNameCN);
                }
                if (sb.Length > 0)
                {
                    return sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }

            }

        }

        /// <summary>
        /// 通过areaID获取Country
        /// </summary>
        public static IEnumerable GetCountryWithArea(int areaID)
        {
            using (var dc = new CCEntities())
            {
                var area = dc.Areas.FirstOrDefault(p => p.AreaID == areaID);
                StringBuilder sb = new StringBuilder();
                area.Countries.Load();
                foreach (var c in area.Countries.Where(p => !p.Disabled))
                {
                    sb.AppendFormat("{0},", c.Name);
                }
                if (sb.Length > 0)
                {
                    return sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }

            }

        }

        /// <summary>
        /// 通过countryID获取Area
        /// </summary>
        public static IEnumerable GetAreaWithCountry(int countryID)
        {
            using (var dc = new CCEntities())
            {
                var country = dc.Countries.FirstOrDefault(p => p.CountryID == countryID);
                StringBuilder sb = new StringBuilder();
                country.Areas.Load();
                foreach (var c in country.Areas.Where(p => !p.Disabled))
                {
                    sb.AppendFormat("{0},", c.Name);
                }
                if (sb.Length > 0)
                {
                    return sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }

            }

        }
        public static IEnumerable GetAreaOverseas(string areaList)
        {
            using (var dc = new CCEntities())
            {
             
                var area = dc.Areas.FirstOrDefault(p => p.Disabled == false);
                StringBuilder sb = new StringBuilder();
                if (!String.IsNullOrEmpty(areaList))
                {
                    var ps = areaList.Split(',');
                    foreach (var c in ps)
                    {

                        if (!String.IsNullOrEmpty(c) && c != "0")
                        {
                            int id = Convert.ToInt32(c);
                            var Name = dc.Areas.FirstOrDefault(p => p.AreaID == id).Name;
                            sb.AppendFormat("{0}, ", Name.ToString());
                        }
                    }
                }
                if (sb.Length > 0)
                {
                    return "; Sales Region=" + sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }

            }
        }
      
        /// <summary>
        /// 通过listID获取level
        /// </summary>
        public static IEnumerable GetLevelWithList(int listID)
        {
            using (var dc = new CCEntities())
            {
                var list = dc.PreferencesLists.FirstOrDefault(p => p.ListID == listID);
                StringBuilder sb = new StringBuilder();
                list.PreferencesLevels.Load();
                foreach (var c in list.PreferencesLevels.Where(p => !p.Disabled))
                {
                    sb.AppendFormat("{0},", c.Name);
                }
                if (sb.Length > 0)
                {
                    return sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }

            }

        }

        /// <summary>
        /// 通过listID获取value
        /// </summary>
        public static IEnumerable GetValueWithList(int listID)
        {
            using (var dc = new CCEntities())
            {
                var list = dc.PreferencesLists.FirstOrDefault(p => p.ListID == listID);
                StringBuilder sb = new StringBuilder();
                list.PreferencesValues.Load();
                foreach (var c in list.PreferencesValues.Where(p => !p.Disabled))
                {
                    sb.AppendFormat("{0},", c.Name);
                }
                if (sb.Length > 0)
                {
                    return sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }

            }

        }

        /// <summary>
        /// 通过companyID获取country
        /// </summary>
        public static IEnumerable GetCountryWithCompany(int companyID)
        {
            using (var dc = new CCEntities())
            {

                var company = dc.Companies.FirstOrDefault(p => p.CompanyID == companyID);
                StringBuilder sb = new StringBuilder();
                company.Countries.Load();
                foreach (var c in company.Countries.Where(p => !p.Disabled))
                {
                    sb.AppendFormat("{0},", c.Name);
                }
                if (sb.Length > 0)
                {
                    return sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }

            }

        }
        /// <summary>
        /// 根据companyID获取Name
        /// </summary>
        public static string GetCompanyWithCustomer(int companyID)
        {
            using (var dc = new CCEntities())
            {
                if (companyID>0)
                {
                    var name = dc.Companies.FirstOrDefault(p => p.CompanyID == companyID).Name;
                    return "; CompanyName=" + name;
                }
                else
                {
                    return "";
                }


            }
        }
        /// <summary>
        /// 根据departmentID获取Name
        /// </summary>
        public static string GetDepartmentName(string departmentid)
        {
            using (var dc = new CCEntities())
            {
                if (!String.IsNullOrEmpty(departmentid))
                {

                    int departmentID = 0;
                    string name = "";
                    if (int.TryParse(departmentid, out departmentID))
                    {
                        var dep = dc.CustomerDepartments.FirstOrDefault(p => p.DepartmentID == departmentID);
                        if (dep != null)
                            name = dep.Name;
                    }
                    else name = departmentid;
                    return "; Department=" + name;
                }
                else
                {
                    return "";
                }
               
            }
        }

        public static string GetProvinceC(string provinceid)
        {
            using (var dc = new CCEntities())
            {
                if (!String.IsNullOrEmpty(provinceid))
                {

                    int provinceID = Convert.ToInt32(provinceid);
                    var name = dc.Provinces.FirstOrDefault(p => p.ProvinceID == provinceID).Name;
                    return "; Province\\CAT=" + name;
                }
                else
                {
                    return "";
                }

            }
        }
        public static string GetCountryO(string country)
        {
            using (var dc = new CCEntities())
            {
                if (!String.IsNullOrEmpty(country))
                {

                    int countryID = Convert.ToInt32(country);
                    var name = dc.Countries.FirstOrDefault(p => p.CountryID == countryID).Name;
                    return "; Country=" + name;
                }
                else
                {
                    return "";
                }

            }
        }
        public static string GetCATC(string cat)
        {
            using (var dc = new CCEntities())
            {
                if (!String.IsNullOrEmpty(cat))
                {

                    int catID = Convert.ToInt32(cat);
                    var name = dc.Categories.FirstOrDefault(p => p.CateID == catID).Name;
                    return  name;
                }
                else
                {
                    return "";
                }

            }
        }
       
        public static string GetBirthday(Customer customer)
        {
            if (customer.Birthday.HasValue)
            {
                return "; Birthday=" + customer.Birthday.GetValueOrDefault().ToShortDateString();
            }
            else
            {
                return "";
            }
        }
        public static string GetTitle(string title)
        {
            if (!String.IsNullOrEmpty(title))
            {
                return "; Title=" + title;
            }
            else
            {
                return "";
            }
        }
        public static string GetAddressC(string name)
        {
            if (!String.IsNullOrEmpty(name))
            {
                return "; Address=" + name;
            }
            else
            {
                return "";
            }
        }
        public static string GetTelephoneC(string name)
        {
            if (!String.IsNullOrEmpty(name))
            {
                return "; Telephone =" + name;
            }
            else
            {
                return "";
            }
        }
        public static string GetFaxC(string name)
        {
            if (!String.IsNullOrEmpty(name))
            {
                return "; Fax =" + name;
            }
            else
            {
                return "";
            }
        }
        public static string GetWebsiteC(string name)
        {
            if (!String.IsNullOrEmpty(name))
            {
                return "; Website =" + name;
            }
            else
            {
                return "";
            }
        }
        public static string GetZipcodeC(string name)
        {
            if (!String.IsNullOrEmpty(name))
            {
                return "; Zipcode =" + name;
            }
            else
            {
                return "";
            }
        }
      
        public static string GetGender(string gender)
        {
            if (!String.IsNullOrEmpty(gender))
            {
                if (gender=="true")
                {
                    return "; Gender=Male ";
                }
                else
                {
                  return  "; Gender=Female ";
                }
            }
            else
            {
                return "";
            }
        }
        public static string GetCellPhone(string cellPhone)
        {
            if (!String.IsNullOrEmpty(cellPhone))
            {
                return "; CellPhone=" + cellPhone;
            }
            else
            {
                return "";
            }
        }
        public static string GetEmail(string email)
        {
            if (!String.IsNullOrEmpty(email))
            {
                return "; Email=" + email;
            }
            else
            {
                return "";
            }
        }
        public static string GetAddress(string address)
        {
            if (!String.IsNullOrEmpty(address))
            {
                return "; Address=" + address;
            }
            else
            {
                return "";
            }
        }
        public static string GetDescription(string description)
        {
            if (!String.IsNullOrEmpty(description))
            {
                return "; Comment=" + description;
            }
            else
            {
                return "";
            }
        }
        public static string GetLevel(string level)
        {
            if (!String.IsNullOrEmpty(level)&&level!="0")
            {
                return "; Level=" + level;
            }
            else
            {
                return "";
            }
        }
   
     

        public static string GetTraining(int customerID,string name)
        {
            using (var dc = new CCEntities())
            {
                if (customerID > 0 && !String.IsNullOrEmpty(name))
                {
                    var customer = dc.Customers.FirstOrDefault(e => e.Disabled == false && e.CustomerID == customerID);
                    customer.CustomerTrainings.Load();

                    IEnumerable<CustomerTraining> customerTraining;
                    customerTraining = customer.CustomerTrainings;
                    //
                    string companyName = string.Join(",", customerTraining.Select(r => r.Name).ToArray());
                  
                    string startTime = string.Join(",", customerTraining.Select(r => r.Date.ToShortDateString().ToString()).ToArray());
                    string endTime = string.Join(",", customerTraining.Select(r => r.EndDate.ToShortDateString().ToString()).ToArray());
                    return "; CustomerTraining: " + "CompanyName=" + companyName + "; StartTime=" + startTime + "; EndTime=" + endTime;
                }
                else
                {
                    return "";
                }
            }
        }

        public static string GetWorkExperience(int customerID, string name)
        {
            using (var dc = new CCEntities())
            {
                if (customerID > 0 && !String.IsNullOrEmpty(name))
                {
                    var customer = dc.Customers.FirstOrDefault(e => e.Disabled == false && e.CustomerID == customerID);
                    customer.WorkExperience.Load();
                  
                    IEnumerable<WorkExperience> workExperiences;
                    workExperiences = customer.WorkExperience;
                    //
                    string companyName = string.Join(",", workExperiences.Select(r => r.CompanyName).ToArray());
                    string title = string.Join(",", workExperiences.Select(r => r.Title).ToArray());
                    string startTime = string.Join(",", workExperiences.Select(r => r.StartTime.GetValueOrDefault().ToShortDateString().ToString()).ToArray());
                    string endTime = string.Join(",", workExperiences.Select(r => r.EndTime.GetValueOrDefault().ToShortDateString().ToString()).ToArray());
                    return "; WorkExperience: " + "CompanyName=" + companyName + "; Title=" + title + "; StartTime=" + startTime + "; EndTime=" + endTime;
                }
                else
                {
                    return "";
                }
            }
        }
        public static string GetEducation(int customerID, string name)
        {
            using (var dc = new CCEntities())
            {
                if (customerID > 0 && !String.IsNullOrEmpty(name))
                {
                    var customer = dc.Customers.FirstOrDefault(e => e.Disabled == false && e.CustomerID == customerID);
                    customer.Education.Load();

                    IEnumerable<Education> educations;
                    educations = customer.Education;

                    string school = string.Join(",", educations.Select(r => r.School).ToArray());
                    string degree = string.Join(",", educations.Select(r => r.Degree).ToArray());
                    string startTime = string.Join(",", educations.Select(r => r.StartTime.GetValueOrDefault().ToShortDateString().ToString()).ToArray());
                    string endTime = string.Join(",", educations.Select(r => r.EndTime.GetValueOrDefault().ToShortDateString().ToString()).ToArray());
                    return "; Education: " + "School=" + school + "; Degree=" + degree + "; StartTime=" + startTime + "; EndTime=" + endTime;
                }
                else
                {
                    return "";
                }
            }
        }
        public static string GetFamilyInfo(int customerID,string name)
        {
            using (var dc = new CCEntities())
            {
                if (customerID > 0 && !String.IsNullOrEmpty(name))
                {
                    var customer = dc.Customers.FirstOrDefault(e => e.Disabled == false && e.CustomerID == customerID);
                    customer.FamilyInfo.Load();

                    IEnumerable<FamilyInfo> familyInfos;
                    familyInfos = customer.FamilyInfo;

                    string fname = string.Join(",", familyInfos.Select(r => r.Name).ToArray());
                    string relationship = string.Join(",", familyInfos.Select(r => r.Relationship).ToArray());
                    string workPlace = string.Join(",", familyInfos.Select(r => r.WorkPlace.ToString()).ToArray());
                    return "; FamilyInfo: " + "Name=" + fname + "; Relationship=" + relationship + "; WorkPlace=" + workPlace;
                }
                else
                {
                    return "";
                }
            }
        }
       
        /// <summary>
        /// 根据WorkExperienceID获取Title
        /// </summary>
        public static IEnumerable GetWorkExperienceTitle(int customerID)
        {
            using (var dc = new CCEntities())
            {
                var customer = dc.Customers.FirstOrDefault(p => p.CustomerID == customerID);
                return customer.WorkExperience.Select(we => we.Title);
            }
        }
       
        /// <summary>
        /// 根据ListID获取listName
        /// </summary>
        public static IEnumerable GetList(int customerID)
        {
            using (var dc = new CCEntities())
            {
                var customer = dc.Customers.FirstOrDefault(p => p.CustomerID == customerID);

                var listID = customer.CustomerPreference.FirstOrDefault(p => p.CustomerID == customerID).ListID;

                var listName = dc.PreferencesLists.FirstOrDefault(p => p.ListID == listID).Name;
                return listName;
            }
        }
        /// <summary>
        /// 根据levelID获取Description
        /// </summary>
        public static IEnumerable GetLevel(int customerID)
        {
            using (var dc = new CCEntities())
            {
                var customer = dc.Customers.FirstOrDefault(p => p.CustomerID == customerID);
                var levelID = customer.CustomerPreference.FirstOrDefault(p => p.CustomerID == customerID).LevelID;
                var levelName = dc.PreferencesLevels.FirstOrDefault(p => p.LevelID == levelID).Name;
                return levelName;
            }
        }
        /// <summary>
        /// 根据valueID获取Description
        /// </summary>
        public static IEnumerable GetValue(int customerID)
        {
            using (var dc = new CCEntities())
            {
                var customer = dc.Customers.FirstOrDefault(p => p.CustomerID == customerID);
                var valueID = customer.CustomerPreference.FirstOrDefault(p => p.CustomerID == customerID).ValueID;
                var valueName = dc.PreferencesValues.FirstOrDefault(p => p.ValueID == valueID).Name;
                return valueName;
            }
        }

        /// <summary>
        /// CID RoleAssignMent GetIsAdminD log
        /// </summary>
        public static IEnumerable GetIsAdminD(string employeeNo)
        {

            using (var dc = new CCEntities())
            {
                if (!String.IsNullOrEmpty(employeeNo))
                {
                    var employee = dc.Employees.FirstOrDefault(e => e.IsEnabled == true && e.EmployeeNo == employeeNo);
                    employee.Roles.Load();
                    if (employee.Roles.Count(r => !r.Disabled && r.BelongsTo == "CID" && r.IsAdmin) > 0)
                        return "Yes";
                    else
                        return "NO";
                }
                else
                {
                    return "";
                }
            }
        }

        /// <summary>
        /// CIO RoleAssignMent GetIsAdminD log
        /// </summary>
        public static IEnumerable GetIsAdminO(string employeeNo)
        {

            using (var dc = new CCEntities())
            {
                if (!String.IsNullOrEmpty(employeeNo))
                {
                    var employee = dc.Employees.FirstOrDefault(e => e.IsEnabled == true && e.EmployeeNo == employeeNo);
                    employee.Roles.Load();
                    if (employee.Roles.Count(r => !r.Disabled && r.BelongsTo == "CIO" && r.IsAdmin) > 0)
                        return "Yes";
                    else
                        return "NO";
                }
                else
                {
                    return "";
                }
            }
        }


        /// <summary>
        /// CID RoleAssignMent GetRolesD log
        /// </summary>
        public static string GetRolesD(string employeeNo)
        {
            using (var dc = new CCEntities())
            {
                if (!String.IsNullOrEmpty(employeeNo))
                {
                    var employee = dc.Employees.FirstOrDefault(e => e.IsEnabled == true && e.EmployeeNo == employeeNo);
                    string belongsTo = "CID";
                    employee.Roles.Load();
                    IEnumerable<Role> roles;
                    roles = employee.Roles.Where(r => !r.Disabled && r.BelongsTo == belongsTo && r.Description != "+pcat+");
                    return string.Join(",", roles.Select(r => r.Name).ToArray());
                }
                else
                {
                    return "";
                }
            }
        }

        /// <summary>
        /// CID RoleAssignMent GetRolesD log
        /// </summary>
        public static string GetModelCIS(string nameCN)
        {
            using (var dc = new CCEntities())
            {
                if (!String.IsNullOrEmpty(nameCN))
                {
                    var employee = dc.Employees.FirstOrDefault(e => e.IsEnabled == true && e.NameCN == nameCN);
                    //string belongsTo = "CID";
                    employee.Roles.Load();
                    var belongsto = employee.Roles.Where(r => !r.Disabled);
                    string belong = belongsto.Select(r => r.BelongsTo).ToString();
                    return belong;
                }
                else
                {
                    return "";
                }
            }
        }



        /// <summary>
        /// CIO RoleAssignMent GetRolesO log
        /// </summary>
        public static string GetRolesO(string employeeNo)
        {
            using (var dc = new CCEntities())
            {
                if (!String.IsNullOrEmpty(employeeNo))
                {
                    var employee = dc.Employees.FirstOrDefault(e => e.IsEnabled == true && e.EmployeeNo == employeeNo);
                    string belongsTo = "CIO";
                    employee.Roles.Load();
                    IEnumerable<Role> roles;
                    roles = employee.Roles.Where(r => !r.Disabled && r.BelongsTo == belongsTo && r.Description != "+country+");
                    return string.Join(",", roles.Select(r => r.Name).ToArray());
                }
                else
                {
                    return "";
                }
            }
        }

        /// <summary>
        /// CIO RoleAssignMent GetIsAdminAll log
        /// </summary>
        public static IEnumerable GetIsAdminAll(string employeeNo, string sys)
        {

            using (var dc = new CCEntities())
            {
                if (!String.IsNullOrEmpty(employeeNo))
                {
                    var employee = dc.Employees.FirstOrDefault(e => e.IsEnabled == true && e.EmployeeNo == employeeNo);
                    employee.Roles.Load();
                    if (employee.Roles.Count(r => !r.Disabled && sys.Contains(r.BelongsTo) && r.IsAdmin) > 0)
                        return "Yes";
                    else
                        return "NO";
                }
                else
                {
                    return "";
                }
            }
        }

        /// <summary>
        /// CIO RoleAssignMent GetRolesAll log
        /// </summary>
        //public static string GetRolesAll(string employeeNo, string sys)
        //{
        //    using (var dc = new CCEntities())
        //    {
        //        if (employeeNo != null)
        //        {
        //            var employee = dc.Employees.FirstOrDefault(e => e.IsEnabled == true && e.EmployeeNo == employeeNo);
        //            //string belongsTo = "CIO";
        //            employee.Roles.Load();
        //            IEnumerable<Role> roles;
        //            roles = employee.Roles.Where(r => !r.Disabled && sys.Contains(r.BelongsTo));
        //            return string.Join(",", roles.Select(r => r.Name).ToArray());
        //        }
        //        else
        //        {
        //            return "";
        //        }
        //    }
        //}

        public static IEnumerable GetRolesAll(string cil, string roleName)
        {
            using (var dc = new CCEntities())
            {
                var user = dc.Employees.FirstOrDefault(p => p.NameEN == cil);
                StringBuilder sb = new StringBuilder();
                if (!String.IsNullOrEmpty(roleName))
                {
                    var ps = roleName.Split(',');
                    foreach (var c in ps)
                    {

                        if (!String.IsNullOrEmpty(c) && c != "0")
                        {
                            int id = Convert.ToInt32(c);
                            var Name = dc.Roles.FirstOrDefault(p => p.RoleID == id).Name;
                            sb.AppendFormat("{0}, ", Name.ToString());
                        }
                    }
                }
                if (sb.Length > 0)
                {
                    return sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }

            }
        }


        /// <summary>
        /// CIO RoleAssignMent GetCluster log
        /// </summary>
        public static IEnumerable GetCountryO(string cil, string country)
        {
            using (var dc = new CCEntities())
            {
                var user = dc.Employees.FirstOrDefault(p => p.NameEN == cil);
                var clust = dc.Roles.FirstOrDefault(p => p.Disabled == false && p.BelongsTo == "CID" && p.Name == "CID Cluster");
                StringBuilder sb = new StringBuilder();
                if (!String.IsNullOrEmpty(country))
                {
                    var ps = country.Split(',');
                    foreach (var c in ps)
                    {

                        if (!String.IsNullOrEmpty(c) && c != "0")
                        {
                            int id = Convert.ToInt32(c);
                            var Name = dc.Countries.FirstOrDefault(p => p.CountryID == id).Name;
                            sb.AppendFormat("{0}, ", Name.ToString());
                        }
                    }
                }
                if (sb.Length > 0)
                {
                    return "; Country=" + sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }

            }
        }


        /// <summary>
        ///CID RoleAssignMent GetCommercialP log
        /// </summary>
        public static IEnumerable GetCommercialP(string cil, string commercialp)
        {

            using (var dc = new CCEntities())
            {
                var user = dc.Employees.FirstOrDefault(p => p.NameEN == cil);
                var commercial = dc.Roles.FirstOrDefault(p => p.Disabled == false && p.BelongsTo == "CID" && p.Name == "CID Commercial");
                var roles = dc.Roles.Where(p => p.Description == "+pcat+" && p.Disabled == false).ToArray();
                StringBuilder sb = new StringBuilder();

                commercial.PCATs.Load();
                if (!String.IsNullOrEmpty(commercialp) && commercialp != "0")
                {
                    var ps = commercialp.Split(',');

                    foreach (var c in ps)
                    {

                        if (!String.IsNullOrEmpty(c) && c != "0")
                        {
                            int provinceID = Convert.ToInt32(c);
                            var provinceName = dc.Provinces.FirstOrDefault(p => p.ProvinceID == provinceID).Name;
                            sb.AppendFormat("{0}, ", provinceName.ToString());
                        }

                    }
                }
                if (sb.Length > 0)
                {
                    return "; Commercial=" + sb.Remove(sb.Length - 2, 2).ToString();

                }
                else
                {
                    return sb.ToString();
                }


            }
        }

        /// <summary>
        /// CID RoleAssignMent GetCommercialC log
        /// </summary>
        public static IEnumerable GetCommercialC(string cil, string commercialc)
        {
            using (var dc = new CCEntities())
            {
                var user = dc.Employees.FirstOrDefault(p => p.NameEN == cil);
                var commercial = dc.Roles.FirstOrDefault(p => p.Disabled == false && p.BelongsTo == "CID" && p.Name == "CID Commercial");
                StringBuilder sb = new StringBuilder();
                commercial.PCATs.Load();
                if (!String.IsNullOrEmpty(commercialc) && commercialc != "0")
                {
                    var ps = commercialc.Split(',');
                    foreach (var c in ps)
                    {

                        if (!String.IsNullOrEmpty(c) && c != "0")
                        {
                            int cATID = Convert.ToInt32(c);
                            var cATName = dc.Categories.FirstOrDefault(p => p.CateID == cATID).Name;
                            sb.AppendFormat("{0}, ", cATName.ToString());
                        }
                    }
                }
                if (sb.Length > 0)
                {
                    return sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }
            }
        }

        /// <summary>
        /// CID RoleAssignMent GetCluster log
        /// </summary>
        public static IEnumerable GetCluster(string cil, string cluster)
        {
            using (var dc = new CCEntities())
            {
                var user = dc.Employees.FirstOrDefault(p => p.NameEN == cil);
                var clust = dc.Roles.FirstOrDefault(p => p.Disabled == false && p.BelongsTo == "CID" && p.Name == "CID Cluster");
                StringBuilder sb = new StringBuilder();
                if (!String.IsNullOrEmpty(cluster))
                {
                    var ps = cluster.Split(',');
                    foreach (var c in ps)
                    {

                        if (!String.IsNullOrEmpty(c) && c != "0")
                        {
                            int id = Convert.ToInt32(c);
                            var Name = dc.Clusters.FirstOrDefault(p => p.ClusterID == id).Name;
                            sb.AppendFormat("{0}, ", Name.ToString());
                        }
                    }
                }
                if (sb.Length > 0)
                {
                    return "; Cluster=" + sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }

            }
        }

        /// <summary>
        /// CID RoleAssignMent GetDPSO log
        /// </summary>
        public static IEnumerable GetDPSO(string cil, string dpso)
        {
            using (var dc = new CCEntities())
            {
                var user = dc.Employees.FirstOrDefault(p => p.NameEN == cil);
                var pSO = dc.Roles.FirstOrDefault(p => p.Disabled == false && p.BelongsTo == "CID" && p.Name == "CID PSO");
                StringBuilder sb = new StringBuilder();
                pSO.PCATs.Load();
                if (!String.IsNullOrEmpty(dpso) && dpso != "0")
                {
                    var ps = dpso.Split(',');
                    foreach (var c in ps)
                    {

                        if (!String.IsNullOrEmpty(c) && c != "0")
                        {
                            int id = Convert.ToInt32(c);
                            var name = dc.Provinces.FirstOrDefault(p => p.ProvinceID == id).Name;
                            sb.AppendFormat("{0}, ", name.ToString());
                        }
                    }
                }
                if (sb.Length > 0)
                {
                    return "; PSO=" + sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }
            }
        }

        /// <summary>
        /// CID RoleAssignMent GetPSO log
        /// </summary>
        public static IEnumerable GetPSO(string cil, string pso)
        {
            using (var dc = new CCEntities())
            {
                var user = dc.Employees.FirstOrDefault(p => p.NameEN == cil);
                var pSO = dc.Roles.FirstOrDefault(p => p.Disabled == false && p.BelongsTo == "CID" && p.Name == "CID PSO");
                StringBuilder sb = new StringBuilder();
                pSO.PCATs.Load();
                if (!String.IsNullOrEmpty(pso) && pso != "0")
                {
                    var ps = pso.Split(',');
                    foreach (var c in ps)
                    {

                        if (!String.IsNullOrEmpty(c) && c != "0")
                        {
                            int id = Convert.ToInt32(c);
                            var name = dc.Provinces.FirstOrDefault(p => p.ProvinceID == id).Name;
                            sb.AppendFormat("{0}, ", name.ToString());
                        }
                    }
                }
                if (sb.Length > 0)
                {
                    return "; PSO=" + sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }
            }
        }


        /// <summary>
        /// CID RoleAssignMent GetSales log
        /// </summary>
        public static IEnumerable GetSales(string cil, string salesp, string salesc)
        {
            using (var dc = new CCEntities())
            {

                var user = dc.Employees.FirstOrDefault(p => p.NameEN == cil);
                var sale = dc.Roles.FirstOrDefault(p => p.Disabled == false && p.BelongsTo == "CID" && p.Name == "CID Sales");

                StringBuilder sb = new StringBuilder();
                sale.PCATs.Load();
                if (!String.IsNullOrEmpty(salesp) && !String.IsNullOrEmpty(salesc) && salesp != "0" && salesc != "0")
                {
                    var ps = salesp.Split(',');
                    var cs = salesc.Split(',');

                    for (int i = 0; i < ps.Length; i++)
                    {
                        if (!String.IsNullOrEmpty(ps[i]) && !String.IsNullOrEmpty(cs[i]))
                        {
                            int pID = Convert.ToInt32(ps[i]);
                            int cID = Convert.ToInt32(cs[i]);
                            var provicne = dc.Provinces.FirstOrDefault(p => p.ProvinceID == pID).Name;
                            var category = dc.Categories.FirstOrDefault(p => p.CateID == cID).Name;

                            sb.AppendFormat("{0}{1}, ", provicne.ToString(), category.ToString());
                        }
                    }
                }
                if (sb.Length > 0)
                {
                    return "; Sales=" + sb.Remove(sb.Length - 2, 2).ToString();
                }
                else
                {
                    return sb.ToString();
                }

            }
        }

    }
}
